//
//  CallMeTypeTableViewCell.m
//  JiuLe
//
//  Created by xcq on 15/11/11.
//  Copyright © 2015年 Fine. All rights reserved.
//

#import "Costants.h"
#import "UserDataMarco.h"
#import "NSUserDefaults+Utils.h"
#import "CallMeTypeTableViewCell.h"

#define kAOpen 0xF1
#define kAClose 0xF2
#define kCT 0xF3

#define kBtnAOpen 0xF4
#define kBtnAClose 0xF5
#define kBtnCT 0xF6


@interface CallMeTypeTableViewCell()
@property (weak, nonatomic) IBOutlet UIImageView *typeImg;
@property (weak, nonatomic) IBOutlet UILabel *typeName;
@property (weak, nonatomic) IBOutlet UIImageView *allOpen;
@property (weak, nonatomic) IBOutlet UIButton *allOpenBtn;
@property (weak, nonatomic) IBOutlet UIImageView *allClose;
@property (weak, nonatomic) IBOutlet UIButton *allCloseBtn;
@property (weak, nonatomic) IBOutlet UIImageView *callType;
@property (weak, nonatomic) IBOutlet UIButton *callTypeBtn;
@property (weak, nonatomic) IBOutlet UILabel *callTypeTime;

@end
@implementation CallMeTypeTableViewCell

- (void)awakeFromNib {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self setImgTapEvent];
}

- (void)setImgTapEvent {
    _allOpen.tag = kAOpen;
    _allClose.tag = kAClose;
    _callType.tag = kCT;
    
    _allOpenBtn.tag = kBtnAOpen;
    _allCloseBtn.tag = kBtnAClose;
    _callTypeBtn.tag = kBtnCT;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(tapEvent:)];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                           action:@selector(tapEvent:)];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                           action:@selector(tapEvent:)];
    
    [_allOpen addGestureRecognizer:tap];
    [_allClose addGestureRecognizer:tap1];
    [_callType addGestureRecognizer:tap2];
}

- (void)tapEvent:(UIGestureRecognizer *)getsture {
    UIImageView *img = (UIImageView *)getsture.view;
     if (img.tag == kAOpen) {
        _allOpen.highlighted = YES;
        _allClose.highlighted = NO;
        _callType.highlighted = NO;
//        [dic setObject:@(0) forKey:@"select"];
         _dic.time = @"1";
    }else if (img.tag == kAClose) {
        _allOpen.highlighted = NO;
        _allClose.highlighted = YES;
        _callType.highlighted = NO;
        _dic.time = @"0";
//        [dic setObject:@(1) forKey:@"select"];
    } else {
        _allOpen.highlighted = NO;
        _allClose.highlighted = NO;
        _callType.highlighted = YES;
//        NSArray *arr = [_timeLabel.text componentsSeparatedByString:@" -- "];
        _dic.time = [[_times firstObject] stringByAppendingString:[_times lastObject]];
    }
    
    [self saveDic:_dic];
}

- (void)setSelect:(NSString *)select {
    if (!(select<0)) {
        _select = select;
        JLLog(@"cell.select : %@",_select);
        if ([_select integerValue] == 1) {
            _allOpen.highlighted = YES;
        } else if ([_select integerValue] == 0) {
            _allClose.highlighted = YES;
        } else {
            _callType.highlighted = YES;
        }
    }
}

- (IBAction)seleteOnOffEvetn:(id)sender {
    UIButton *btn = (UIButton *)sender;
     if (btn.tag == kBtnAOpen) {
        _allOpen.highlighted = YES;
        _allClose.highlighted = NO;
        _callType.highlighted = NO;
        _dic.time = @"1";
    } else if (btn.tag == kBtnAClose) {
        _allOpen.highlighted = NO;
        _allClose.highlighted = YES;
        _callType.highlighted = NO;
        _dic.time = @"0";

    } else {
        _allOpen.highlighted = NO;
        _allClose.highlighted = NO;
        _callType.highlighted = YES;
        _dic.time = [[_times firstObject] stringByAppendingString:[_times lastObject]];
    }

    [self saveDic:_dic];
}


- (void)setTimes:(NSArray *)times {
    if (times == nil || times.count == 0) {
        return;
    }
    _times = times;
    NSString *firstTime = [[[[times firstObject] substringWithRange:NSMakeRange(0, 2)] stringByAppendingString:@":"] stringByAppendingString:[[times firstObject] substringWithRange:NSMakeRange(2, 2)]];
    NSString *lastTime = [[[[times lastObject] substringWithRange:NSMakeRange(0, 2)] stringByAppendingString:@":"] stringByAppendingString:[[times lastObject] substringWithRange:NSMakeRange(2, 2)]];
     _timeLabel.text = [NSString stringWithFormat:@"%@ -- %@",firstTime ,lastTime];
}

- (void)saveDic:(JLCallMeModel *)dic {
    NSString *str = nil;
    if (_type == 1) {
        str = UserCallMeTypeSos;
    } else if (_type == 2) {
        str = UserCallMeTypeEarlyWarning;
    } else if (_type == 3){
        str = UserCallMeTypeAlarm;
    } else {
        str = UserCallMeTypeFallDown;
    }
    [NSUserDefaults setUserObject:dic forKey:str];
//    postNotification(@"refreshData");
    postNotificationWithObject(@"refreshData", dic);
}

//设置时间
- (IBAction)setDate:(id)sender {
    __weak CallMeTypeTableViewCell *weak = self;
    _action(weak);
}

- (void)setType:(int)type {
    _type = type;
    if (type == 1) {
        _typeImg.image = [UIImage imageNamed:@"home_jj"];
        _typeName.text = @"紧急呼叫";
    } else if (type == 2) {
        _typeImg.image = [UIImage imageNamed:@"ld_sos"];
        _typeName.text = @"预警";
    } else if (type == 3){
        _typeImg.image = [UIImage imageNamed:@"home_bj"];
        _typeName.text = @"报警";
    } else {
        _typeImg.image = [UIImage imageNamed:@"ld_dd"];
        _typeName.text = @"跌倒";
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
